import aiohttp
import asyncio
import json
import utils


URL = 'http://127.0.0.1:3000/dcs/upload/spindle_status'
#loop = None

async def task_push_spindles_status_to_cloud(loop=None):
    """ 将本地 mysql数据库 的数据 push 到 阿里云 postgres """

    utils.print_threading_info()

    # 1 先实现上传
    data = {
        "a":100,
        "b":200
    }
    data = json.dumps(data)
    async  with aiohttp.ClientSession() as session:
        response = await session.post(URL, data=data)
        res = await response.read()
        print(res)

    # 2 改进：实现过滤后再上传
    loop.call_later(5, task_push_spindles_status_to_cloud, loop)

def function_tmp(loop=None):
    asyncio.create_task(task_push_spindles_status_to_cloud)


def upload_main():

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    #loop.call_soon(task_push_spindles_status_to_cloud(loop))
    loop.call_soon(function_tmp, loop)
    try:
        loop.run_forever()
    finally:
        loop.close()


if __name__ == "__main__":

    upload_main()